package com.haizhi.crm.biz.service.impl;

import com.haizhi.crm.biz.dao.ConcernedGroupDao;
import com.haizhi.crm.biz.model.ConcernedGroupDo;
import com.haizhi.crm.biz.model.ConcernedGroupQo;
import com.haizhi.crm.biz.service.ConcernedGroupService;
import com.haizhi.crm.common.constant.BizStatus;
import com.haizhi.crm.common.exception.UnexpectedStatusException;
import com.haizhi.crm.common.model.Response;
import com.haizhi.crm.common.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by yangyijun on 2018/6/25.
 */
@Service
public class ConcernedGroupServiceImpl implements ConcernedGroupService {

    @Autowired
    private ConcernedGroupDao concernedGroupDao;

    @Transactional
    @Override
    public Response save(ConcernedGroupQo concernedGroupQo) {
        try {
            ConcernedGroupDo concernedGroupDo = new ConcernedGroupDo();
            concernedGroupDo.setUserId(CommonUtils.getUserID());
            concernedGroupDo.setEnabledFlag("1" );
            concernedGroupDo.setCreatedDt(new Date());
            concernedGroupDo.setCreatedById(CommonUtils.getUserID());
            concernedGroupDo.setName(concernedGroupQo.getName());
            concernedGroupDo.setRemark(concernedGroupQo.getRemark());
            concernedGroupDo.setUpdatedDt(new Date());
            concernedGroupDo.setUpdateById(CommonUtils.getUserID());
            concernedGroupDao.save(concernedGroupDo);
            return Response.success(concernedGroupDo);
        } catch (Exception e) {
            throw new UnexpectedStatusException(BizStatus.SAVE_CONCERN_GROUP_ERROR, e);
        }
    }

    @Override
    public Response list() {
        try {
            return Response.success(concernedGroupDao.findByUserId(CommonUtils.getUserID()));
        } catch (Exception e) {
            throw new UnexpectedStatusException(BizStatus.FIND_CONCERN_GROUP_ERROR, e);
        }
    }
}
